package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service("1002")
@SuppressWarnings({"all"})
public class TrackStrategyImpl implements ItemTypeStrategy {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;
    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //定义一个计数器
        int count = 0;
        //远程调用获取声音信息
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackById(userPaidRecordVo.getItemIdList().get(0));
        Assert.notNull(trackInfoResult, "声音远程调用失败");
        TrackInfo trackInfo = trackInfoResult.getData();
        Assert.notNull(trackInfo, "声音为空");
        //新增user_paid_track表
        for (Long itemId : userPaidRecordVo.getItemIdList()) {
            UserPaidTrack userPaidTrack =new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrack.setTrackId(itemId);
            count += userPaidTrackMapper.insert(userPaidTrack);
        }
        return count;
    }
}
